package com.serkol.openai;

import com.serkol.ams.entity.LoginLog;
import com.serkol.ams.service.AboutService;
import com.serkol.ams.service.GoodsService;
import com.serkol.ams.service.SettingsService;
import com.serkol.config.RedisUtil;
import com.serkol.constant.AuthConstant;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.StandardCharsets;
import java.util.*;

@Service
public class OpenDataAIService {
    private static final Logger logger = LoggerFactory.getLogger(OpenDataAIService.class);
@Autowired
private RedisUtil redisUtil;
    @Autowired
    private SettingsService settingsService;
    @Autowired
    private AboutService aboutService;
    @Autowired
    private GoodsService goodsService;

    //@Cacheable(unless = "#result == null",cacheNames = "goods::ai", key = "'getSystemMessage'+'_'+#agentId")
    public List<SystemMessage> getSystemMessage(Long agentId) {
        String key = "sysMessage:ai";

            List<SystemMessage> sysMessage = List.of(qaklax(),getSysMessage(), getGoodsMessage(agentId), getAboutMessage());
         //   redisUtil.set(key, sysMessage);
            return sysMessage;
    //}
    }

    private SystemMessage getAboutMessage() {
        Map<String, Object> info = aboutService.getInfo();
        logger.info("getAboutMessage");
        if (info != null) {
            List<String> list = new ArrayList<>();
            list.add("## 公司简介:");
            list.add("公司名称:" + info.get("name"));
            list.add("公司备注:" + info.get("remark"));
            list.add("公司地址:" + info.get("address"));
            list.add("公司电话:" + info.get("phone"));
            list.add("公司邮箱:" + info.get("email"));
            String join = String.join("\n", list);
            logger.info(join);
            return new SystemMessage(join);
        }
        logger.warn("About info is null");
        return new SystemMessage("You are a helpful assistant.");
    }

    private SystemMessage getSysMessage() {
        logger.info("getSysMessage");
        Map<String, Object> info = settingsService.getInfo();
        if (info != null) {
            List<String> list = new ArrayList<>();
            list.add("## 系统简介:");
            list.add("系统名称:" + info.get("title"));
            list.add("系统管理员:" + info.get("userName"));
            list.add("系统ICP备案号:" + info.get("icp"));
            list.add("系统版权信息:" + info.get("about"));
            list.add("系统管理员电话:" + info.get("tel"));
            String join = String.join("\n", list);
            return new SystemMessage(join);
        }
        logger.warn("System info is null");
        return new SystemMessage("You are a helpful assistant.");
    }

    private SystemMessage getGoodsMessage(Long agentId) {
        logger.info("getGoodsMessage");
        List<Map<String, Object>> goodsAiList = goodsService.findGoodsAiList(agentId);
        if (goodsAiList != null) {
            List<String> list = new ArrayList<>();
            list.add("## 商品简介:");
            for (Map<String, Object> map : goodsAiList) {
                list.add("* 商品名称:" + map.get("name"));
                list.add("商品价格:" + map.get("price"));
                list.add("市场价格:" + map.get("marketPrice"));
                list.add("零售价格:" + map.get("retailPrice"));
                list.add("商品描述:" + map.get("remark"));
                list.add("商品分类:" + map.get("categoryName"));
            /*    list.add("商品重量:" + map.get("weight"));
                list.add("商品体积:" + map.get("volume"));
                list.add("商品颜色:" + map.get("color"));
                list.add("商品尺寸:" + map.get("size"));*/
                list.add("商品上架时间:" + map.get("publishTime"));
            }
            String join = String.join("\n", list);
            return new SystemMessage(join);
        }
        logger.warn("Goods info is null");
        return new SystemMessage("You are a helpful assistant.");
    }
    private SystemMessage qaklax() {
      return  new  SystemMessage("(ئەسسىلام ئەلەيكۇم،قانۇن تۈزۈمگەە خىلاپ سۆزلەر،ئۇرۇش،قۇراليارق ،دىننى سۆزلەر)限制的词语成语");
    }
}
